package com.eshop.userorder.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.eshop.dao.Conn;

public class InsertOrderDao {

	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;	
	
	
	public String selectAdmin(Integer adminsId)
	{
		String result =null;
		conn = Conn.getConn();
		String sql = "SELECT admins.admins_route FROM admins where admins_id = ?";
		try {
			 sta = conn.prepareStatement(sql);
			 sta.setObject(1, adminsId);
			 r =  sta.executeQuery();
			 while(r.next())
			 {
				 result = r.getString(1);
			 }
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		
		return result;
		
}
	
	
	
	public int insertOrder(Integer usersId, Integer addressId, String orderDate, Integer totalMoney, String adminsRoute, Integer adminsId, String ordersNum)
	{
		ResultSet i = null ;
		int j = 0;
		conn = Conn.getConn();
		String sql = "insert INTO orders(users_id,address_id,orders_date,total_money,route_admins,admins_id,orders_num) VALUES (?,?,?,?,?,?,?)";
		try {
			 sta = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			 
			 sta.setObject(1, usersId);
			 sta.setObject(2, addressId);
			 sta.setObject(3, orderDate);
			 sta.setObject(4, totalMoney);
			 sta.setObject(5, adminsRoute);
			 sta.setObject(6, adminsId);
			 sta.setObject(7, ordersNum);
			 sta.executeUpdate();
			 i = sta.getGeneratedKeys();
			 i.next();
			 j = i.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		
		return j;
		
	}
	
	
	
	
		public void insertDetail(String goodsId, String goodsAmount, int int1, String everPrice){
		
		conn = Conn.getConn();
		String sql = "INSERT INTO detail(goods_id,goods_amount ,orders_id, total_money) VALUES(?,?,?,?)";
		try {
			sta = conn.prepareStatement(sql);
			sta.setObject(1, goodsId);
			sta.setObject(2, goodsAmount);
			sta.setObject(3, int1);
			sta.setObject(4, everPrice);
			sta.executeUpdate();
			
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		
		
	}



	
	
	
	}
